// pages/projectDealPerson/projectDealPerson.js
const app = getApp();
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project_detail: {},
    thePosterFileInfo: {},
    thePosterInfo: {},
    isShowMask: false,
    noticeInfo: {
      phrase3: {
        value: "同意"
      },
      thing8: {
        value: ""
      }
    },
  },

  // -----------------------------------------------------------------------------
  // 刷新面试邀请信息
  inputChange(e) {
    this.setData({
      ['noticeInfo.' + e.target.id + '.value']: e.detail.value
    });
    // console.log(this.data.noticeInfo);
  },
  // 收回
  withdraw() {
    var that = this;
    that.setData({
      isShowMask: false
    });
  },

  // 发送通知
  sendNotice() {
    wx.showLoading({
      title: '请求中',
    });
    var that = this;
    const templateId = 'OIkMRaN10nsqkgpppwYqcy30VFxlOicD9fZHHWxkvd8' //模板ID
    // 查询订阅消息表
    db.collection('wantJoinSubscribe').where({
      _openid: that.data.thePosterInfo._openid,
      status: 0,
      templateId: templateId,
      project_id: that.data.project_detail._id
    }).limit(1).get({
      success: res => {
        if (res.data.length != 0) {
          console.log(res.data[0]);
          const id = res.data[0]._id; //简写一下该条记录的id
          //本地修改返回来的记录的subMsg
          that.setData({
            ['noticeInfo.thing8.value']: '【' + that.data.project_detail.name + '】:' + that.data.noticeInfo.thing8.value
          })
          res.data[0].subMsg = that.data.noticeInfo;
          console.log(res.data[0].subMsg);
          // 调用云函数发送订阅消息通知
          wx.cloud.callFunction({
            name: "subscribe",
            data: res.data[0],
            success: res2 => {
              console.log(res2);
              if (res2.result.errCode == 47003) {
                wx.showModal({
                  content: "信息填写不规范",
                  showCancel: false,
                })
                console.log(res2.result.errMsg);
                return
              } else if (res2.result.errCode == 43101) {
                console.log('用户拒绝接受消息，如果用户之前曾经订阅过，则表示用户取消了订阅关系')
                wx.showToast({
                  icon: "none",
                  title: '用户拒绝收到消息',
                })
                return
              } else if (res2.result.errCode == 41030) {
                console.log('page路径不正确，需要保证在现网版本小程序中存在，与 app.json 保持一致')
                return
              } else {
                //本地数据刷新
                app.navTmpInfo = that.data.thePosterFileInfo;
                that.setData({
                  ['thePosterFileInfo.status']: '已同意'
                })
                //修改订阅记录状态和发送信息
                db.collection('wantJoinSubscribe').doc(id).update({
                  data: {
                    subMsg: that.data.noticeInfo,
                    status: 1
                  },
                  success: res3 => {
                    console.log("wantJoinSubscribe表修改成功")
                  },
                  fail: res3 => {
                    console.log(res3);
                  }
                });

                // 修改wantJoinFile记录状态
                db.collection('wantJoinFile').where({
                  project_id: that.data.thePosterFileInfo.project_id,
                  _openid: that.data.thePosterInfo._openid,
                  timestamp: that.data.thePosterFileInfo.timestamp
                }).update({
                  data: {
                    status: "已同意"
                  },
                  success: function (res) {
                    that.setData({
                      isShowMask: false
                    })
                    wx.showToast({
                      title: "同意成功",
                      icon: 'success',
                      mask: true,
                    })
                    console.log("wantJoinFile表修改成功")
                  },
                  fail: function () {
                    console.log("wantJoinFile未修改成功");
                  }
                })
              }

            },
            fail: res => {
              console.log(res);
              wx.showToast({
                icon: 'none',
                title: '云函数调用失败'
              })
            },
            complete: () => {
              wx.hideLoading();
            }
          })
        } else {
          console.log("wantJoinSubscribe订阅表中没有此记录");
          wx.showToast({
            icon: "none",
            title: '数据异常',
          })
        }
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          title: '查询订阅表失败',
        })
      }
    });

  },

  agree() {
    var that = this;
    wx.showModal({
      content: '确定同意该学僧加入项目？',
      success(res) {
        if (res.confirm) {
          that.setData({
            isShowMask: true
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    })
  },
  getRefuse() {
    var that = this;
    wx.showModal({
      content: "确定拒绝该学僧？",
      success(res) {
        if (res.confirm) {
          db.collection('wantJoinFile').where({
            _openid: that.data.thePosterInfo._openid,
            project_id: that.data.thePosterFileInfo.project_id,
            timestamp: that.data.thePosterFileInfo.timestamp
          }).update({
            data: {
              status: '已拒绝'
            },
            success: function (res) {
              wx.showToast({
                title: "已拒绝",
                icon: 'success',
              })
            },
            fail: function () {
              console.log("拒绝失败");
            }
          })
        }
      }
    })


  },
  // 预览简历
  previewResume() {
    let that = this;
    wx.cloud.getTempFileURL({
      fileList: [that.data.thePosterFileInfo.fileID],
      success: res => {
        // console.log(res);
        // console.log(res.fileList[0].tempFileURL);
        wx.showLoading({
          title: '加载中',
        })
        wx.downloadFile({
          url: res.fileList[0].tempFileURL,
          success: res => {
            // console.log(res)
            wx.openDocument({
              filePath: res.tempFilePath,
              success: res => {
                console.log('预览成功');
              },
              fail: res => {
                console.log('预览失败');
              }
            })
          },
          fail: res => {
            wx.showToast({
              title: '网络错误',
              icon: "error"
            })
          },
          complete: () => {
            wx.hideLoading();
          }
        })

      }
    });
  },


  navBack() {
    wx.navigateBack({
      delta: 1
    });
  },

  // -------------------------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      thePosterFileInfo: app.navTmpInfo.thePosterFileInfo,
      thePosterInfo: app.navTmpInfo.thePosterInfo,
      project_detail: app.navTmpInfo.project_detail
    })
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})